#include "dechexbin.h"
#include <string.h>

#define T_W 4 /*Table width*/
#define T_H 3 /*Table height*/

#define HEX "HEX"
#define DEC "DEC"
#define DEC_R ""
#define BIN "BIN"
#define BE "BE"

/* Table structure
   --------------------------------------
     | c1 | c2   | c3             | c4
   --------------------------------------
   r1| DEC| Entry| Be: Big Endian | Entry
   r2| HEX| Entry| Be             | Entry
   r3| BIN| Entry| Be             | Entry
   --------------------------------------
   */
n_entry * create_number_entry(GtkWidget * frame)
{
	/*Create several number entry here*/	
	/*Use table to manage layout*/

	GtkWidget *table;
	table = gtk_table_new(T_H,T_W,FALSE);
	gtk_container_add (GTK_CONTAINER (frame), table);
	gtk_widget_show(table);
	n_entry * number_entry = malloc(sizeof(n_entry));
	if(NULL == number_entry){
		perror("malloc for n_entry");
		exit(1);
	}
	number_entry->value = 0LL;
	/*Create table item now*/
	number_entry->dec_label = gtk_label_new(DEC);
	gtk_widget_show(number_entry->dec_label);

	number_entry->dec_right_label = gtk_label_new(DEC_R);
	gtk_widget_show(number_entry->dec_right_label);

	number_entry->hex_label = gtk_label_new(HEX);
	gtk_widget_show(number_entry->hex_label);

	number_entry->bin_label = gtk_label_new(BIN);
	gtk_widget_show(number_entry->bin_label);

	number_entry->hex_be_label = gtk_label_new(BE);
	gtk_widget_show(number_entry->hex_be_label);

	number_entry->bin_be_label = gtk_label_new(BE);
	gtk_widget_show(number_entry->bin_be_label);

	number_entry->dec_entry = gtk_entry_new();
	gtk_widget_show(number_entry->dec_entry);

	number_entry->hex_le_entry = gtk_entry_new();
	gtk_widget_show(number_entry->hex_le_entry);

	number_entry->hex_be_entry = gtk_entry_new();
	gtk_widget_show(number_entry->hex_be_entry);

	number_entry->bin_le_entry = gtk_entry_new();
	gtk_widget_show(number_entry->bin_le_entry);

	number_entry->bin_be_entry = gtk_entry_new();
	gtk_widget_show(number_entry->bin_be_entry);

	/* Attach to table */
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->dec_label,
					0,1,0,1);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->dec_entry,
					1,2,0,1);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->dec_right_label,
					2,4,0,1);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->hex_label,
					0,1,1,2);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->hex_le_entry,
					1,2,1,2);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->hex_be_label,
					2,3,1,2);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->hex_be_entry,
					3,4,1,2);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->bin_label,
					0,1,2,3);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->bin_le_entry,
					1,2,2,3);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->bin_be_label,
					2,3,2,3);
	gtk_table_attach_defaults(GTK_TABLE(table),number_entry->bin_be_entry,
					3,4,2,3);

	return number_entry;
};

n_entry * create_number1_entry(GtkWidget * frame)
{
	n_entry * e;
	e = create_number_entry(frame);
	gtk_label_set_text(GTK_LABEL(e->dec_right_label),"BE = Big Endian");
	gtk_editable_set_editable(GTK_EDITABLE(e->dec_entry),TRUE);
	gtk_editable_set_editable(GTK_EDITABLE(e->hex_be_entry),TRUE);

	gtk_editable_set_editable(GTK_EDITABLE(e->hex_le_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->bin_le_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->bin_be_entry),FALSE);
	GdkColor color;
	gdk_color_parse ("yellow", &color);
	gtk_widget_modify_base (e->hex_le_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->bin_le_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->bin_be_entry, GTK_STATE_NORMAL, &color);
	return e;
};

n_entry * create_number2_entry(GtkWidget * frame)
{
	n_entry * e;
	e = create_number_entry(frame);
	gtk_editable_set_editable(GTK_EDITABLE(e->dec_entry),TRUE);
	gtk_editable_set_editable(GTK_EDITABLE(e->hex_be_entry),TRUE);

	gtk_editable_set_editable(GTK_EDITABLE(e->hex_le_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->bin_le_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->bin_be_entry),FALSE);
	GdkColor color;
	gdk_color_parse ("yellow", &color);
	gtk_widget_modify_base (e->hex_le_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->bin_le_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->bin_be_entry, GTK_STATE_NORMAL, &color);
	return e;
};

n_entry * create_result_entry(GtkWidget * frame)
{
	n_entry * e;
	e = create_number_entry(frame);
	gtk_editable_set_editable(GTK_EDITABLE(e->dec_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->hex_le_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->hex_be_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->bin_le_entry),FALSE);
	gtk_editable_set_editable(GTK_EDITABLE(e->bin_be_entry),FALSE);
	GdkColor color;
	gdk_color_parse ("yellow", &color);
	gtk_widget_modify_base (e->dec_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->hex_le_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->hex_be_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->bin_le_entry, GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (e->bin_be_entry, GTK_STATE_NORMAL, &color);
	return e;
};


